package xin.basic.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.basic.model.Dict;
import xin.basic.model.DictGroup;
import xin.basic.repository.DictGroupRepository;
import xin.basic.repository.DictRepository;
import xin.common.http.request.NamePageRequest;
import xin.common.http.response.PageResponse;
import xin.common.http.response.PageWrapper;
import xin.common.utils.AssertUtils;
import xin.common.utils.StringUtils;
import xin.core.ErrorConstant;
import xin.core.exception.SysException;
import xin.core.jpa.JpaColnumConstant;
import xin.core.jpa.JpaConstant;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 * 数据字典服务
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2018/8/10
 */
@Service
@Transactional(readOnly = true)
public class DictService {

    @Autowired
    private DictRepository repository;

    @Autowired
    private DictGroupRepository groupRepository;


    public PageResponse findPageList(NamePageRequest request ){
        Sort sort = new Sort(Sort.Direction.ASC,JpaColnumConstant.NAME);
        Pageable pageable = new org.springframework.data.domain.PageRequest(request.getPageNumber()-1,request.getPageSize(),sort);
        Specification<DictGroup> specification = (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if(StringUtils.isNotBlank(request.getName())){
                predicates.add(criteriaBuilder.like(root.get(JpaColnumConstant.NAME),
                        JpaConstant.WILDCARD + request.getName() + JpaConstant.WILDCARD));
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        return PageWrapper.wrap(groupRepository.findAll(specification, pageable));
    }

    public Dict findDict(String dictId){
        return repository.findOne(dictId);
    }

    @Transactional
    public Dict saveDict(Dict dict){
        return repository.save(dict);
    }

    @Transactional
    public void deleteDict(String dictId){
        repository.delete(dictId);
    }

    public DictGroup findDictGroup(String dictGroupCode){
        return groupRepository.findOne(dictGroupCode);
    }

    @Transactional
    public DictGroup saveDictGroup(DictGroup dictGroup){
        return groupRepository.save(dictGroup);
    }

    @Transactional
    public void deleteDictGroup(String dictGroupCode){
        if(AssertUtils.isNotNull(repository.findByGroupCode(dictGroupCode))){
            groupRepository.delete(dictGroupCode);
        }else{
            throw new SysException(ErrorConstant.ERROR_DATA_0002,"当前字典表分组分在组内记录，请先删除组内记录或改变组内记录的分组");
        }
    }
}
